// index.js
// 获取应用实例
const promiseRequest  = require('../../utils/request.js')
const app = getApp()
Page({
  data: {
    navList: [
      {
        label: "直播",
        value: 'BA10TA81wangning'
      },
      {
        label: "推荐",
        value: 'BD2A9LEIwangning'
      },
      {
        label: "热门",
        value: 'BD2AB5L9wangning'
      },
      {
        label: "追番",
        value: 'BD2AC4LMwangning'
      },
      {
        label: "影视",
        value: 'BA8E6OEOwangning'
      },
      {
        label: "建党",
        value: 'BA8EE5GMwangning'
      },
      {
        label: "其他",
        value: 'BAI67OGGwangning'
      }
    ],
    navListData: {

    },
    navActive: 1,
    refresherTriggered: false
  },
  onLoad() {
    let navList = []
    for(let i = 0; i < 30; i++ ) {
      navList = [...navList, ...this.data.navList]
    }
    this.setData({
      navList
    })
    this.setInitData()
    this.onSearch();
    // console.log(Object.keys(this.data.navListData));
  },
  setInitData: function() {
    const navListData = {}
    this.data.navList.forEach(item => {
      navListData[item.value] = {
        list: [],
        pageObj: {
          page: 0,
          pageSize: 20
        },
        scrollTop: 0,
        key: item.value,
        nav: item
      }
    });
    this.setData({
      navListData
    });
  },
  getCurrentItem: function() {
    return this.data.navList[this.data.navActive];
  },
  getCurrentItemData: function() {
    const currentItem = this.getCurrentItem();
    return this.data.navListData[currentItem.value];
  },
  onNavChange: function(e) {
    this.onSetScrollTop()
    // return false;
    this.setData({
      navActive: e.detail
    });
    const currentItemData = this.getCurrentItemData()
    if (currentItemData.list.length === 0) {
      this.onSearch()
    }
  },
  onSwiperChnage: function(e) {
    console.log(e);
    if (e.detail.source !== 'touch') {
      return false
    }
    this.onSetScrollTop()
    // return false;
    this.setData({
      navActive: e.detail.current
    })
    const currentItemData = this.getCurrentItemData()
    if (currentItemData.list.length === 0) {
      this.onSearch()
    }
  },
  onScrollToLower: function(e) {
    console.log('onScrollToLower');
    const currentItemData = this.getCurrentItemData();
    this.setData({
      [`navListData.${currentItemData.key}.pageObj.page`]: currentItemData.pageObj.page + 1
    })
    this.onSearch();
  },
  onScrollTop: function(e) {
    console.log('onScrollTop');
    if (this.onSearchIng) {
      return false
    }
    this.onSearchIng = true;
    const currentItemData = this.getCurrentItemData();
    this.setData({
      [`navListData.${currentItemData.key}.pageObj.page`]: 0
    })
    this.setData({
      refresherTriggered: true
    })
    this.onSearch();
  },
  onScrollEnd: function(e) {
    console.log(e);
    const currentItemData = this.getCurrentItemData();
    this.setData({
      [`navListData.${currentItemData.key}.scrollTop`]: e.detail.scrollTop
    })
  },
  onSetScrollTop: function(e) {
    const currentItemData = this.getCurrentItemData()
    const query = wx.createSelectorQuery()
    query.select(`#${currentItemData.key}`).fields({
      scrollOffset: true
    })
    query.exec(res => {
      console.log('onSetScrollTop', res);
      if ( res[0]) {
        this.setData({
          [`navListData.${currentItemData.key}.scrollTop`]: res[0].scrollTop
        })
      }
    })
  },
  onSearch: function(e) {
    const url = 'https://3g.163.com/touch/reconstruct/article/list/';
    const currentItemData = this.getCurrentItemData();
    const type = currentItemData.key;
    const start = currentItemData.pageObj.page * currentItemData.pageObj.pageSize;
    const len = currentItemData.pageObj.pageSize
    promiseRequest.requestFn(`${url}${type}/${start}-${len}.html`).then(res => {
      const rex = /(?<=artiList\().+(?=\))/;
      let dataJson = res.data && res.data.match(rex) ? res.data.match(rex)[0] : '';
      const data = JSON.parse(dataJson);
      // console.log('res', data[currentItemData.key]);
      if (currentItemData.pageObj.page === 0) {
        this.setData({
          [`navListData.${currentItemData.key}.list`]: data[currentItemData.key]
        })
      } else {
        let dataList = [];
        let len = currentItemData.list.length
        data[currentItemData.key].forEach(item => {
          dataList[`navListData.${currentItemData.key}.list[${len++}]`] = item
        });
        this.setData(dataList)
      }
    }).finally(res => {
      this.onSearchIng = false;
      this.setData({
        refresherTriggered: false
      })
    })
    // wx.request({
    //   url: `${url}${type}/${start}-${len}.html`,
    //   success: (res) => {
    //     const rex = /(?<=artiList\().+(?=\))/;
    //     let dataJson = res.data && res.data.match(rex) ? res.data.match(rex)[0] : '';
    //     const data = JSON.parse(dataJson);
    //   }
    // })
  }
})
